﻿using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Windows.Forms;
using System.Drawing;
using System.Runtime.InteropServices;

namespace HandRise.MBHOP.Controls
{
    public class DialogList : Component
    {


        /// <summary>
        /// 数据源

        /// </summary>
        public Object DataSource
        {
            get
            {
                return listForm.dataSource;
            }
            set
            {
                listForm.dataSource = value;
                //DataBind();
            }
        }

        /// <summary>
        /// 要显示的属性

        /// </summary>
        public string DisplayMember
        {
            get
            {
                return listForm.displayMember;
            }
            set
            {
                listForm.displayMember = value;
            }
        }

        /// <summary>
        /// 主键
        /// </summary>
        public string KeyMember
        {
            get
            {
                return listForm.keyMember;
            }
            set
            {
                listForm.keyMember = value;
            }
        }

        /// <summary>
        /// 默认值

        /// </summary>
        public string DefaultValue
        {
            get
            {
                return listForm.defaultValue;
            }
            set
            {
                listForm.defaultValue = value;
            }
        }

        /// <summary>
        /// 弹起时的按钮背景色

        /// </summary>
        public Color ItemBackColor
        {
            set
            {
                listForm.itemBackColor = value;
            }
            get
            {
                return listForm.itemBackColor;
            }
        }

        /// <summary>
        /// 按下时的按钮背景色

        /// </summary>
        public Color SelectItemBackColor
        {
            set
            {
                listForm.selectItemBackColor = value;
            }
            get
            {
                return listForm.selectItemBackColor;
            }
        }

        /// <summary>
        /// 弹起时的文本字号
        /// </summary>
        [DefaultValue(12)]
        public int ItemFontSize
        {
            set
            {
                listForm.itemFontSize = value;
            }
            get
            {
                return listForm.itemFontSize;
            }
        }

        /// <summary>
        /// 按下时的文本字号
        /// </summary>
        [DefaultValue(12)]
        public int SelectItemFontSize
        {
            set
            {
                listForm.selectItemFontSize = value;
            }
            get
            {
                return listForm.selectItemFontSize;
            }
        }

        /// <summary>
        /// 弹起时的文本颜色
        /// </summary>
        public Color ItemForeColor
        {
            set
            {
                listForm.itemForeColor = value;
            }
            get
            {
                return listForm.itemForeColor;
            }
        }

        /// <summary>
        /// 按下时的文本颜色
        /// </summary>
        public Color SelectItemForeColor
        {
            set
            {
                listForm.selectItemForeColor = value;
            }
            get
            {
                return listForm.selectItemForeColor;
            }
        }

        /// <summary>
        /// 每页的数量

        /// </summary>
        [DefaultValue(4)]
        public int PageSize
        {
            get
            {
                return listForm.pageSize;
            }
            set
            {
                if (value < 0)
                    value = 0;
                listForm.pageSize = value;
            }
        }

        ListForm listForm = new ListForm();

        PictureBox pictureBox = new PictureBox();

        public DialogListItem ShowList(Form parentForm, object dataSource, string displayMember, string keymember, string defaultValue)
        {
            listForm.dataSource = dataSource;
            listForm.displayMember = displayMember;
            listForm.keyMember = keymember;
            listForm.defaultValue = defaultValue;
            return ShowList(parentForm);
        }

        public DialogListItem ShowList(Form parentForm, object dataSource, string displayMember, string keymember)
        {
            return this.ShowList(parentForm, dataSource, displayMember, keymember, "");
        }

        public DialogListItem ShowList(Form parentForm)
        {
            if (PageSize <= 0)
                return new DialogListItem();
            if (parentForm.WindowState == FormWindowState.Maximized)
                listForm.Text = "";
            else
            {
                listForm.WindowState = parentForm.WindowState;
                listForm.MaximizeBox = parentForm.MaximizeBox;
                listForm.Text = parentForm.Text;
                listForm.Menu = null;
            }

            pictureBox.Dock = DockStyle.Fill;
            pictureBox.SizeMode = PictureBoxSizeMode.Normal;
            pictureBox.Image = PrtSc(parentForm);
            parentForm.Controls.Add(pictureBox);
            parentForm.Controls.SetChildIndex(pictureBox, 0);
            listForm.ShowDialog();
            parentForm.Controls.Remove(pictureBox);
            return listForm.returnValue;
        }

        private static Bitmap PrtSc(Form parentForm)
        {
            //获得当前屏幕的大小

            Rectangle rect = new Rectangle();
            rect = Screen.PrimaryScreen.Bounds;
            //创建一个以当前屏幕为模板的图象
            Graphics g1 = parentForm.CreateGraphics();

            //创建以屏幕大小为标准的位图 
            Bitmap MyImage = new Bitmap(rect.Width, rect.Height);
            Graphics g2 = Graphics.FromImage(MyImage);
            //得到屏幕的DC
            IntPtr dc1 = g1.GetHdc();
            //得到Bitmap的DC 
            IntPtr dc2 = g2.GetHdc();
            //调用此API函数，实现屏幕捕获

            BitBlt(dc2, 0, 0, rect.Width, rect.Height, dc1, 0, 0, 13369376);

            PlatformAPI.DrawAlpha(dc2, HandRise.MBHOP.Controls.ResImage.back, 100, 0, 0);

            //释放掉屏幕的DC
            g1.ReleaseHdc(dc1);
            //释放掉Bitmap的DC 
            g2.ReleaseHdc(dc2);

            return MyImage;

        }

        [DllImport("coredll.dll")]
        private static extern bool BitBlt(
         IntPtr hdcDest, //目标设备的句柄 
         int nXDest, // 目标对象的左上角的X坐标 
         int nYDest, // 目标对象的左上角的X坐标 
         int nWidth, // 目标对象的矩形的宽度 
         int nHeight, // 目标对象的矩形的长度 
         IntPtr hdcSrc, // 源设备的句柄 
         int nXSrc, // 源对象的左上角的X坐标 
         int nYSrc, // 源对象的左上角的X坐标 
         System.Int32 dwRop // 光栅的操作值 
         );

    }
}
